package ext.tianma.part.validators;
import java.util.Locale;

import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.type.TypedUtility;
import wt.util.WTException;

import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;



public class TwoRawTransRawValidator extends DefaultUIComponentValidator{
	private static final String D_VIEW = "Design";
	
	public UIValidationResultSet performFullPreValidation(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria, Locale locale) throws WTException {
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		boolean flag = false;
		
		if (uivalidationcriteria != null && uivalidationcriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationcriteria.getContextObject();
			wt.fc.Persistable persistable = wtreference.getObject();
			WTUser curUser = (WTUser) SessionHelper.manager.getPrincipal();
			WTPart part = (WTPart) persistable;
			if(isObjectType(part,"SemiFinishedGood")||isObjectType(part,"C4")||isObjectType(part,"CF")||isObjectType(part,"RawMaterial")||isObjectType(part,"Polarizer")||isObjectType(part,"PhantomPolarizer"))
			{
				String state = part.getLifeCycleState().getStringValue();
				
				//显示入口新添加一个状态“INWORK”
				if(state.contains("NewProduct")||state.contains("MassProductionRelease")||state.contains("APPROVAL") || state.contains("INWORK"))
				{
					
						flag=true;
		
					
				}
				//add kwang 2013-12-04 如果不是“Design”入口不显示
				String view = part.getViewName();
				if( ( view != null ) && ( !view.equals( D_VIEW ) ) ){
					flag = false;
				}
			}
		}
		if (flag) {
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.ENABLED));
		} else {
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.HIDDEN));
		}
		return uivresultSet;
	}

	/**
	 *判断对象类型是否为指定的内部关键字类型
	 * @param object 对象；
	 * @param object 内部关键字；
	 * @return boolean  是指定的类型返回true；不是返回false
	 */
	public static boolean isObjectType(WTObject object,String objectType)
	{
		if(objectType.equals("") || objectType==null||object==null)
			return false;
		TypeIdentifier typeidentifier = TypedUtility.getTypeIdentifier(object);
		String typeName= typeidentifier.getTypename();
		if(typeName.indexOf(objectType)>=0)
		{
			return true;
		}
		return false;
	}

}
